package com.xiaoq.route.sites.repository.impl;


import com.xiaoq.route.sites.entity.RoutePointSitesEntity;
import com.xiaoq.route.sites.entity.SitesRouteEntity;
import com.xiaoq.route.sites.repository.ISitesRouteRepository2;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class SitesRouteRepository2Impl implements ISitesRouteRepository2 {

    @Autowired
    private Datastore dataStore;

    @Override
    public void addSitesRoute(SitesRouteEntity sitesRoute) {
        dataStore.save(sitesRoute);
    }

    @Override
    public List<SitesRouteEntity> findSitesRoute(String cityCode) {
        List<SitesRouteEntity> sitesRoutes = dataStore.find(SitesRouteEntity.class).field("zoneCity").equal(cityCode).asList();
        return sitesRoutes;
    }

    @Override
    public SitesRouteEntity findSitesRouteDetail(String id) {
        SitesRouteEntity sitesRoute = dataStore.find(SitesRouteEntity.class).field("_id").equal(id).get();
        return sitesRoute;
    }

    @Override
    public void deleteSitesRoute(String id) {

        Query<SitesRouteEntity> query =  dataStore.find(SitesRouteEntity.class).field("_id").equal(id);
        dataStore.delete(query);

    }

    @Override
    public void savePointRoute(RoutePointSitesEntity routePoint) {
        dataStore.save(routePoint);
    }

    @Override
    public List<RoutePointSitesEntity> findPointRoute(String routeId) {
        Query<RoutePointSitesEntity> query =  dataStore.find(RoutePointSitesEntity.class).field("routeId").equal(routeId).order("sortNum");
        return query.asList();
    }

    @Override
    public void deleteRouteSites(String routeId, String sitesId) {
        Query<RoutePointSitesEntity> query =  dataStore.find(RoutePointSitesEntity.class).field("routeId").equal(routeId).field("sitesId").equal(sitesId);
        dataStore.delete(query);

    }

    @Override
    public void changeSites(String routeId, String sitesId, Integer status) {
        RoutePointSitesEntity routePoint  =  dataStore.find(RoutePointSitesEntity.class).field("routeId").equal(routeId).field("sitesId").equal(sitesId).get();
        Integer sort = routePoint.getSortNum();
        Integer sort2 = null;
        if(1==status){
            sort2 = sort;
            RoutePointSitesEntity routePoint1=dataStore.find(RoutePointSitesEntity.class).field("routeId").equal(routeId).filter("sortNum <",sort).order("-sortNum").offset(0).limit(1).get();
            routePoint.setSortNum(routePoint1.getSortNum());
            routePoint1.setSortNum(sort2);
            dataStore.save(routePoint);
            dataStore.save(routePoint1);

        }else if(0==status){
            sort2 = sort;
            RoutePointSitesEntity routePoint1 = dataStore.find(RoutePointSitesEntity.class).field("routeId").equal(routeId).filter("sortNum >",sort).order("sortNum").offset(0).limit(1).get();
            routePoint.setSortNum(routePoint1.getSortNum());
            routePoint1.setSortNum(sort2);
            dataStore.save(routePoint);
            dataStore.save(routePoint1);
        }

    }
}
